package com.gitee.feizns.webtemplate.commons.upload.processor.impl;

import com.gitee.feizns.webtemplate.commons.upload.MediaType;
import com.gitee.feizns.webtemplate.commons.upload.UploadEvent;
import com.gitee.feizns.webtemplate.commons.upload.UploadProperties;
import com.gitee.feizns.webtemplate.commons.upload.processor.Processor;
import com.gitee.feizns.webtemplate.commons.utils.ftp.FtpService;

import java.util.Map;

/**
 * @author feizns
 * @since 2019/5/15
 */
public class FtpDirProcessor implements Processor {

    private FtpService ftpService;

    public FtpDirProcessor(FtpService ftpService) {
        this.ftpService = ftpService;
    }

    @Override
    public Map<MediaType, String[]> getProperty(UploadProperties uploadProperties) {
        return uploadProperties.getFtpUploadDirs();
    }

    @Override
    public void write(UploadEvent event, String dir, UploadProperties properties) {
        boolean result = ftpService.uploadFile(dir, event.getFilename(), event.getInputStream());
        if ( result == false )
            throw new RuntimeException("上传失败.");
    }

}
